package gofish.servlets;

import gofish.json.JsonObject;
import gofish.objects.GameData;

/**
 * TakeTurn will be a Servlet used by a client to take a turn in a game of
 * Go Fish.  A turn consists of asking one other player if they have a
 * certain card in their hand. 
 * @author Matt Lauber
 *
 */
public class TakeTurn {
	
	/**
	 *  These are the possible responses that can be returned to a client.
	 */
	private static final int ILLEGAL_TURN = -1;
	private static final int PLAYER_HAD_CARD = 0;
	private static final int FISHED_CORRECT_CARD = 1;
	private static final int FISHED_DIFFERENT_PAIR = 2;
	private static final int FISHED_NOTHING = 3;

	/**
	 * doPost() is called by a client to take a turn in a game of Go Fish. 
	 * @param gid identifies the game in which you are participating
	 * @param userName is the name of the person doing the asking
	 * @param target is the name of the other player you wish to ask
	 * @param card is a 2-character representation of the card you are asking the other player for
	 * @return an integer representing the response code (see instance variables above)
	 */
	public int doPost(int gid, String userName, String target, String card) {
		
		JsonObject response = new JsonObject();
		int code; // will hold the response code
		

		// If the player asks someone else for a card that the asking player
		//  doesn't even have, then it's an illegal turn.
		if (!GameData.isCardInPlayerHand(gid, userName, card)) {
			code = ILLEGAL_TURN;
			System.out.println("Illegal turn");
		}
		// If it is a legal turn...
		else {
			// If the player being asked also has the card, then take it
			//   from them and put it in the asking player's pairs.
			//   The asking player will get to go again.
			if (GameData.isCardInOpponentHand(gid, target, card)) {
				code = PLAYER_HAD_CARD;
				GameData.removeCardFromPlayerHand(gid, target, card);
				GameData.removeCardFromPlayerHand(gid, userName, card);
				GameData.addCardToPlayerPairs(gid, userName, card);
				System.out.println("Player had card");
			}
			// If the player being asked does not have the card...
			else {
				// ... go fish!
				String fishCard = GameData.goFish(gid);
				System.out.println("fished: " + fishCard);
				
				// If you fished the correct card, add it to your pairs and go again.
				if (fishCard.charAt(0) == card.charAt(0)) {
					code = FISHED_CORRECT_CARD;
					GameData.removeCardFromPlayerHand(gid, userName, card);
					GameData.addCardToPlayerPairs(gid, userName, card);
					System.out.println("fished correct card");
				}
				// If you fished a card that matches a DIFFERENT card in your
				//   hand, then put it with your pairs, but don't go again.
				else if (GameData.isCardInOpponentHand(gid, userName, fishCard)) {
					code = FISHED_DIFFERENT_PAIR;
					GameData.removeCardFromPlayerHand(gid, userName, fishCard);
					GameData.addCardToPlayerPairs(gid, userName, fishCard);
					GameData.nextTurn(gid);
					System.out.println("fished different pair");
				}
				// If the card you fished does not help you at all, put it
				//   in your hand.
				else {
					code = FISHED_NOTHING;
					GameData.addCardToPlayerHand(gid, userName, fishCard);
					GameData.nextTurn(gid);
					System.out.println("fished nothing");
				}
			}
		}
		
		
		response.add("response", code);
		System.out.println(response); // for testing purposes

		// If the deck is empty, or either of the two players involved
		//  now have empty hands, then tell the DB that this game is
		//  now over.
		if (GameData.isDeckEmpty(gid)
				|| GameData.isPlayerHandEmpty(gid, userName)
				|| GameData.isPlayerHandEmpty(gid, target)) {
			GameData.endGame(gid);
		}
		// If the game is not over and it was not an illegal turn,
		//   then increment the turn counter.
		else if (code != ILLEGAL_TURN) {
			GameData.incrementEid(gid);
		}
		
		return code; // This will be removed in the next phase
	}
}
